﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
using TeamDeployer.TeamFoundation.Build.Workflow.Design;

namespace TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes
{
    [Serializable, TypeConverter(typeof(DeploySpecConverter)), DefaultProperty("Name"), Editor(
        typeof(DeploySpecEditor), typeof(UITypeEditor))]
    public class DeploySpec
    {
        // Methods
        public DeploySpec()
        {
            this.Enabled = true;
        }
       
        [DefaultValue(false), DisplayName("Allow Untrusted SSL"), Description(
            "Allow untrusted SSL certificate when using https in the Destination Service URL."), Category(
            "Target"), RefreshProperties(RefreshProperties.All)]
        public bool AllowUntrusted { get; set; }

        [DisplayName("Configuration"), RefreshProperties(RefreshProperties.All), Category("General"), Description(
            "Specifies which configuration run to create the package for.")]
        public string Configuration { get; set; }

        [RefreshProperties(RefreshProperties.All), DisplayName("Destination Service URL"), Description(
            "The destination service url.\nExample: https://<DestinationServer>:8172/MSDeploy.axd "), Category(
            "Target")]
        public string DestinationServiceURL { get; set; }

        [DefaultValue(true), RefreshProperties(RefreshProperties.All), DisplayName("Enabled"), Category(
            "General")]
        public bool Enabled { get; set; }
        [Browsable(false)]
        public bool HasPackageToDeploy
        {
            get
            {
                return !string.IsNullOrEmpty(this.RelativePackagePath);
            }
        }
        [Browsable(false)]
        public Guid Id { get; set; }

        [RefreshProperties(RefreshProperties.All), Category("General"), DisplayName("Name")]
        public string Name { get; set; }

        [RefreshProperties(RefreshProperties.All), DisplayName("File Name"), Description(
            "The file name of the package to be created during the build process.\nExample:web_package.deploy.cmd"), Category(
            "Package")]
        public string PackageName { get; set; }

        [DisplayName("Relative Path"), Category("Package"), RefreshProperties(RefreshProperties.All), Description(
            "The relative path to the deploy package.\nExample: _PublishedWebsites\\web_package\\")]
        public string PackagePath { get; set; }

        [Category("Target"), RefreshProperties(RefreshProperties.All), DisplayName("Password"), PasswordPropertyText(
            true)]
        public string Password { get; set; }
        [Browsable(false)]
        public string RelativePackagePath
        {
            get
            {
                return Path.Combine(this.PackagePath, this.PackageName);
            }
        }
        [RefreshProperties(RefreshProperties.All), DisplayName("Simulate Deployment ?"), Description(
            "Simulates deployment. This does not deploy the package. Instead, it creates a report of what will happen when you actually deploy the package."), DefaultValue(
            false), Category("Target")]
        public bool Trial { get; set; }

        [DisplayName("User Name"), RefreshProperties(RefreshProperties.All), Category("Target")]
        public string UserName { get; set; }

        public DeploySpec Clone()
        {
            return new DeploySpec
            {
                AllowUntrusted = this.AllowUntrusted,
                Configuration = this.Configuration,
                DestinationServiceURL = this.DestinationServiceURL,
                Enabled = this.Enabled, Id = this.Id, Name = this.Name,
                PackageName = this.PackageName,
                PackagePath = this.PackagePath, Password = this.Password, Trial = this.Trial,
                UserName = this.UserName
            };
        }
    }
}